package com.toman.Adpter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Entity.Managemant_Entity;
import com.toman.Interfaces.BaseUrl;
import com.toman.MyApplication;
import com.toman.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import com.squareup.picasso.Picasso;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by  GridView_adpter on Administrator on 2017/11/3 on 16:13.
 */

public class Management_adpter extends BaseAdapter {

    //上下文对象
    Context context;
    //
    ArrayList<Managemant_Entity> arrayList;

    LayoutInflater inflater;

    MyApplication myApplication;

    LinearLayout mAd_bg;
    ImageView mdel_img;


    public Management_adpter(Context context, ArrayList<Managemant_Entity> arrayList, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;
        // if (null == convertView){
        addPackage = new AddPackage();
        convertView = inflater.inflate(R.layout.management_item, null, false);
        addPackage.img = (ImageView) convertView.findViewById(R.id.mManImg);
        addPackage.title = (TextView) convertView.findViewById(R.id.mManTitle);
        mAd_bg = (LinearLayout) convertView.findViewById(R.id.mMan_bg);
        mdel_img = (ImageView) convertView.findViewById(R.id.mMan_Delimg);

//            convertView.setTag(addPackage);
//        }else {
//            addPackage=(AddPackage) convertView.getTag();
//        }

        if (myApplication.getSp().getString("token", "").equals("toman")) {
            mAd_bg.setBackgroundResource(R.color.loginbg);

        } else if (myApplication.getSp().getString("token", "").equals("operater")) {
            mAd_bg.setBackgroundResource(R.color.g7bg);

        } else if (myApplication.getSp().getString("token", "").equals("company")) {
            mAd_bg.setBackgroundResource(R.color.c1);
            //mdel_img.setVisibility(View.VISIBLE);
        }

        if (arrayList.get(position).isok()) {
            mdel_img.setVisibility(View.VISIBLE);
        } else {
            mdel_img.setVisibility(View.INVISIBLE);
        }

        //addPackage.img.setBackgroundResource(arrayList.get(position).getImg());

        if (arrayList.get(position).getImg().equals("")) {
            Picasso.with(context).load(R.mipmap.chanxian).into(addPackage.img);
        } else {
            Picasso.with(context).load(arrayList.get(position).getImg()).into(addPackage.img);
        }

        if (arrayList.get(position).getTitle().equals("")) {
            addPackage.title.setText("未知");
        } else {
            addPackage.title.setText(arrayList.get(position).getTitle());
        }

        mdel_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelDialog(position);

            }
        });

        return convertView;
    }


    public class AddPackage {
        ImageView img;
        TextView title;
    }


    /*
        提示删除
     */
    public void DelDialog(final Integer position) {
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.del_layout1);

        dialog.getWindow().findViewById(R.id.mZxx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.getWindow().findViewById(R.id.mDelQiye).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                del(arrayList.get(position).getId(), position);
                dialog.dismiss();
            }
        });

    }


    /*
       删除企业
     */
    public void del(Integer customer_id, final int po) {
        String token = myApplication.getSp().getString("tokenId", "");
        OkHttpUtils
                .post(BaseUrl.del)
                .params("token", token)
                .params("customer_id", customer_id.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "删除企业: " + result);
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                arrayList.remove(po);
                                notifyDataSetChanged();
                            } else {
                                Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


}
